<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: dataList.all_num }" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange" @sendUserSearch="receiveSearch" :searchData="searchDataInfo" :isHaveExport="false" @toAdd="openAdd">

      <template v-slot:table>
        <el-table ref="multipleTable" :data="dataList.data" stripe v-loading="tableLoading" header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <!-- <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input @change="sortEdit(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off" size="mini"></el-input>
            </template>
          </el-table-column> -->
          <el-table-column prop="picture" min-width="160" label="权益图片">
            <template slot-scope="scope">

              <el-image style="width: 100px; height: 100px" fit="cover" :src="$fnc.getImgUrl(scope.row.m_pic)" :preview-src-list="[$fnc.getImgUrl(scope.row.m_pic)]">
              </el-image>
            </template>
          </el-table-column>

          <el-table-column label="权益信息" min-width="220">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>权益名称：</span>
                  <span>{{ scope.row.m_title || "----" }}</span>
                </div>
                <div>
                  <span>权益分类：</span>
                  <span>{{ scope.row.flid_cn || "----" }}</span>
                </div>
                <div>
                  <span>权益金额：</span>
                  <span>{{Number( scope.row.m_money ) || "---" }}</span>
                </div>
                <div>
                  <span>每天释放比例：</span>
                  <span>{{ Number( scope.row.m_day_production) || "---" }}‰</span>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="权益信息" min-width="160">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>释放天数：</span>
                  <span>{{ scope.row.m_life || "----" }}</span>
                </div>
                <div>
                  <span>获得积分：</span>
                  <span>{{ Number( scope.row.z_money )|| "---" }}</span>
                </div>
                <div>
                  <span>限购数量：</span>
                  <span>{{ scope.row.purchase_limit || "---" }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
         
          <!-- <el-table-column label="用户信息" min-width="120">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>用户昵称：</span>
                  <span>{{ scope.row.nickname || "----" }}</span>
                </div>
                <div>
                  <span>会员账号：</span>
                  <span>{{ scope.row.username || "---" }}</span>
                </div>
              </div>
            </template>
          </el-table-column> -->

          <el-table-column label="激活" min-width="100">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.is_show" active-color="#1aad19" inactive-color="#ffffff" active-text="on" inactive-text="off" active-value="1" @change="switchEdit($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="时间信息" min-width="220">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>添加时间：</span>
                  <span>{{ scope.row.created_time | getTimeFormat }}</span>
                </div>
                <div>
                  <span>修改时间：</span>
                  <span>{{ scope.row.update_time | getTimeFormat }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="openEdit(scope.row)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="moreDelete">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" :title="isEdit ? '编辑' : '添加'" class="tl size_13" width="800px" :visible.sync="dialogFormVisible">
      <!-- <el-row>
        <el-col :span="16"> -->
          <el-form :model="editForm" ref="form" label-width="120px">
            <el-form-item label="权益名称：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.m_title" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="权益分类：">
              <el-col :span="20">
                <el-select v-model="editForm.flid" filterable>
                  <el-option v-for="(item,i) in $store.state.rightsCate" :key="i" :label="item.title" :value="item.id"></el-option>
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="权益图片：" prop="m_pic">
              <el-col :span="20">
                <el-input v-model="editForm.m_pic" disabled></el-input>
                <uploadpic :piclink="editForm.m_pic" :width="150" :height="150" :real_width="200" real_height="auto" :is_more="false" @send_pic="piclinkSuccess"></uploadpic>
              </el-col>
            </el-form-item>
            <el-form-item label="权益金额：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.m_money" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="每天释放比例：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.m_day_production" autocomplete="off">
                  <span slot="append">‰</span>
                </el-input>
              </el-col>
            </el-form-item>
         
            <el-form-item label="获得积分：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.z_money" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="限购数量：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.purchase_limit" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="总规模：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.zmoney1" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="每日获赠收益：">
              <el-col :span="20">
                <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="editForm.mrsyfw" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item>
              <el-button :loading="$store.state.isLoading" type="primary" @click="saveEdit">保 存</el-button>
            </el-form-item>
        </el-form>
        <!--   </el-col>
        <el-col :span="8">
          <div class="black" style="display:flex;">
            <div class="ueditor">
              <ue v-model="editForm.content" v-if="dialogFormVisible" :value="editForm.content" style="width:375px" upload-url="/api/common/upload/index/" fileName="file" />
              <div class="blank"></div>
            </div>

          </div>
        </el-col>
      </el-row> -->

    </el-dialog>
    <!-- <keep-alive>
      <component :is="'xzuser'" :show="selectVisible" :isAll="false" showType="list" @sendShop="selectInfo" @isshow="selectShow"></component>
    </keep-alive> -->
  </div>
</template>
<script>
// import xzuser from "@/components/currency/xz_user.vue";
import ue from "@/components/currency/Ue/ue.vue";
import uploadpic from "@/components/page/pic/up_pic";
import { dzRightsListSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      queryObj: {},
      searchDataInfo: dzRightsListSearchData,
      selectVisible: false,
      isEdit: false,
      dialogFormVisible: false,
      editForm: {
        piclink: "",
      },
      tableLoading: false,
      dataList: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      multipleSelection: [],
    };
  },
  components: {
    // xzuser,
    uploadpic,
    ue,
  },
  methods: {
    selectShow(val) {
      this.selectVisible = val;
    },
    selectInfo(info) {
      this.editForm.username = info.username;
      this.editForm.uid = info.id;
    },
    selectOpen() {
      this.selectVisible = true;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    moreDelete() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            this.$api.getDz.del_rights_list({ id_str: str }).then((res) => {
              if (res.data.code == 200) {
                this.$message.success("删除成功！");
                this.getDataList();
              } else {
                this.$swal("删除失败！", res.data.result, "error");
              }
            });
          }
        });
      }
    },
    openDelete(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getDz.del_rights_list({ id_str: id }).then((res) => {
            if (res.data.code == 200) {
              this.$message.success("删除成功！");
              this.getDataList();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    openEdit(item) {
      this.isEdit = true;
      let info = JSON.parse(JSON.stringify(item));
      info.m_money = info.m_money > 0 ? Number(info.m_money) : "";
      info.m_day_production =
        info.m_day_production > 0 ? Number(info.m_day_production) : "";
      info.z_money = info.z_money > 0 ? Number(info.z_money) : "";
      this.editForm = info;
      this.dialogFormVisible = true;
    },
    openAdd() {
      this.editForm = {
        m_pic: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    piclinkSuccess(pic) {
      this.editForm.m_pic = pic;
    },

    receiveSearch(obj) {
      this.currentPage = 1;
      this.searchAll = obj;
      this.getDataList();
    },
    switchEdit(val, item) {
      var params = item;
      params.is_show = val;
      this.$api.getDz.edit_rights_list(params).then((res) => {
        if (res.data.code != 200) {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getDataList();
        }
      });
    },

    sorta(a, b) {
      return b - a;
    },
    sortEdit(index, item) {
      var params = item;
      params.sort = item.sort;
      this.$api.getDz.edit_rights_list(params).then((res) => {
        if (res.data.code == 200) {
          return;
        } else {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getDataList();
        }
      });
    },
    saveEdit() {
      var params = {};
      params = JSON.parse(JSON.stringify(this.editForm));
      if (this.isEdit == false) {
        this.$api.getDz.add_rights_list(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.currentPage = 1;
            this.getDataList();

            this.$message.success("添加成功！");
          } else {
            this.$swal("添加失败！", res.data.result, "error");
          }
        });
      } else {
        this.$api.getDz.edit_rights_list(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.getDataList();
            this.$message.success("编辑成功！");
          } else {
            this.$swal("编辑失败！", res.data.result, "error");
          }
        });
      }
    },
    getDataList() {
      this.tableLoading = true;
      var params = {};
      params = JSON.parse(JSON.stringify(this.searchAll));
      params.page = this.currentPage || 1;
      params.page_size = this.pageSize || "";
      for (var i in params) {
        if (!params[i]) {
          params[i] = "";
        }
      }
      var query = JSON.parse(JSON.stringify(this.queryObj || {}));
      var obj = Object.assign(query, params);
      this.$api.getDz.get_rights_list(obj).then((res) => {
        if (res.data.code == 200) {
          this.dataList = res.data.result;
        }
        setTimeout(() => {
          this.tableLoading = false;
        }, 200);
      });
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getDataList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList();
    },
  },
  created() {
    if (this.$route.query) {
      this.queryObj = this.$route.query;
      if (this.$route.query.page) {
        this.currentPage = Number(this.$route.query.page);
        delete this.queryObj.page;
      }
    }
    this.getDataList();
    this.$store.dispatch("getRightsCate");
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
